package com.chuyu.jzwboa.activity.launcher;

import android.annotation.TargetApi;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;

import com.chuyu.jzwboa.MainActivity;
import com.chuyu.jzwboa.R;
import com.chuyu.jzwboa.base.MVPBaseActivity;
import com.chuyu.jzwboa.presenter.launcher.LauncherPersenter;
import com.chuyu.jzwboa.view.iview.launcher.ILauncherView;

import java.lang.ref.WeakReference;

/**
 * @author 杨仕俊
 * @description 启动界面  需要检测权限
 * Created by wo on 2017/12/14.
 */

public class LauncherActivity extends MVPBaseActivity<ILauncherView,
        LauncherPersenter> implements ILauncherView {

    private static final int APP_HANDLER = 0x1;
    private static final long SPLASH_DELAY_MILLIS = 3000;
    private static final int REQUEST_CODE = 0; // 请求码
    private static final int REQUEST_CODE_WRITE_SETTINGS = 1;

    // 需检查的全部敏感权限
    static final String[] PERMISSIONS = new String[]{
            "android.permission.READ_PHONE_STATE",
            "android.permission.WRITE_EXTERNAL_STORAGE",
            "android.permission.READ_EXTERNAL_STORAGE",
            "android.permission.CAMERA"
    };
    private MyHandler myHandler;

    @Override
    public void changeActivity(boolean TAG) {
        if (TAG) {
            //有权限
            myHandler.sendEmptyMessageDelayed(APP_HANDLER, SPLASH_DELAY_MILLIS);
        } else {
            startPermissionsActivity();
        }
    }



    @Override
    protected int initContent() {
        return R.layout.activity_launcher;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        myHandler = new MyHandler(this);
        getPresenter().getPermission(this, PERMISSIONS);
    }

    @NonNull
    @Override
    public LauncherPersenter createPresenter() {
        return new LauncherPersenter();
    }

    private void startPermissionsActivity() {
        PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);
    }

    /**
     * 跳转界面
     */
    private void jumpNext() {
        //不是第一次进入
//        if ((Boolean) SPUtils.get(this, SPConstant.FIRST_JOIN,true)){
//            startActivity(new Intent(this,GuideActivity.class));
//        }else if (!(Boolean)SPUtils.get(this,SPConstant.IS_LOGIN,false)){
//            //未登录
//            Intent intent = new Intent(this, LoginActivity.class);
//            startActivity(intent);
//        }else {
//            //主界面
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
//        }
        finish();


    }


    private class MyHandler extends Handler {
        //弱引用
        private WeakReference<LauncherActivity> launcherActivity;

        public MyHandler(LauncherActivity activity) {
            launcherActivity = new WeakReference<LauncherActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            LauncherActivity launcherActivity = this.launcherActivity.get();
            if (launcherActivity != null) {
                switch (msg.what) {
                    case APP_HANDLER:
                        jumpNext();
                        break;
                    default:
                        break;
                }
            }


        }
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 拒绝时, 缺少主要权限, 无法运行
        if (requestCode == REQUEST_CODE && resultCode == PermissionsActivity.PERMISSIONS_DENIED) {
            // ToastUtils.showSure(this,"缺少主要权限，可能影响某些功能的使用！");
            requestWriteSettings();
        } else if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
            if (Settings.System.canWrite(this)) {

                Log.i("test", "onActivityResult write settings granted");
            } else {
                Log.i("test", "onActivityResult write settings unGranted");
            }
            myHandler.sendEmptyMessageDelayed(APP_HANDLER, SPLASH_DELAY_MILLIS);
        } else {
            myHandler.sendEmptyMessageDelayed(APP_HANDLER, SPLASH_DELAY_MILLIS);
        }
    }

    @TargetApi(16)
    private void requestWriteSettings() {
        //可修改系统设置
        Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
        intent.setData(Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, REQUEST_CODE_WRITE_SETTINGS);
    }
}
